package com.gin.no_gay_alliance.adpter;

import android.content.Context;
import android.view.View;

import androidx.annotation.NonNull;

import com.gin.common.utils.TimeUtils;
import com.gin.nga.params.read.ReadReplyParam;
import com.gin.nga.response.field.EditHistory;
import com.gin.no_gay_alliance.activity.TopicReadActivity;
import com.gin.no_gay_alliance.adpter.base.BaseBindingRecyclerViewAdapter;
import com.gin.no_gay_alliance.databinding.ItemEditHistoryBinding;
import com.gin.no_gay_alliance.param.TopicReadParam;

import java.util.List;

/**
 * 编辑历史适配器
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2024/1/20 17:05
 **/
public class EditHistoryAdapter extends BaseBindingRecyclerViewAdapter<ItemEditHistoryBinding, EditHistory> implements View.OnClickListener {
    public EditHistoryAdapter(Context context) {
        super(context);
    }

    public EditHistoryAdapter(@NonNull Context context, @NonNull List<EditHistory> data) {
        super(context, data);
    }

    /**
     * 绑定数据
     *
     * @param holder
     * @param b        绑定对象
     * @param position 位置
     * @param item
     */
    @Override
    protected void onBindViewHolder(ViewHolder<ItemEditHistoryBinding> holder, @NonNull ItemEditHistoryBinding b, int position, EditHistory item) {
        b.getRoot().setText(String.format("%s之前", TimeUtils.format(item.getTimestamp(), TimeUtils.SHORT_FORMATTER_2)));
        b.getRoot().setTag(item);
        b.getRoot().setOnClickListener(this);
    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        EditHistory editHistory = (EditHistory) v.getTag();

        ReadReplyParam param = editHistory.obtainReadReplyParam();
        if (param == null) {
            throw new RuntimeException("参数生成失败");
        }
        TopicReadParam topicReadParam = new TopicReadParam();
        topicReadParam.topicId = param.getTopicId();
        topicReadParam.replyId = param.getReplyId();
        topicReadParam.backupId = param.getBackupId();

        TopicReadActivity.startActivity(context, "编辑历史", topicReadParam);
    }
}
